"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 修改stat文件
Create At   : 2020/12/08
@zou_jialiang0505328126
Description :
    1.检查数据库状态
    2.执行TPCC
    3.TPCC执行过程中修改gaussdb.state二进制文件，增加内容
    4.查看数据库状态是否正常
    5.重启集群
    6.查看gaussdb.state状态
    7.查看pg_log有gaussdb.state更新记录
Expect      :
    1.检查数据库状态正常
    2.TPCC执行成功
    3.修改gaussdb.state二进制文件成功
    4.查看数据库状态正常
    5.重启集群成功
    6.文件更新
    7. update gaussdb sta e file
History     :
     modified: 2020/12/09  modified by @peilinqian,优化断言，增加测试步骤gaussdb.state查询展示
"""

import unittest
import time
import os
from yat.test import macro
from yat.test import Node
import sys
sys.path.append(sys.path[0] + "/../")
from testcase.utils.CommonSH import *
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread


class Chowndn1(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case093.py 开始执行------------')
        self.dbPrimaryDbUser = Node(node='PrimaryDbUser')
        self.dbPrimaryRoot = Node(node='PrimaryRoot')
        self.Constant = Constant()
        self.common = Common('PrimaryRoot')
        self.addtxt = "dddddd#! /bin/sh -"

    def test_basebackup(self):
        self.log.info('------------------------1.检查数据库状态------------------------')
        result = self.commonshpri.getDbClusterStatus("status")
        self.assertTrue(result)

        self.log.info('--------------2.执行TPCC-------------------------------')
        self.log.info('============启动TPCC=================')
        tpcc_thread = ComThread(self.common.startTPCC, args=(self.dbPrimaryRoot, macro.TPCC_PATH,))
        tpcc_thread.setDaemon(True)
        tpcc_thread.start()
        time.sleep(3)

        self.log.info('-------------------------3.TPCC执行过程中修改gaussdb.state二进制文件，增加内容-------------')
        shell_cmd = f'''sed -i "1i {self.addtxt}"  {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')}'''
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.assertIn('', result)

        self.log.info('-------------------------查看gaussdb.state二进制文件-------------')
        shell_cmd = f"cat {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')}"
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('===============检查TPCC结果===============')
        tpcc_thread.join(60 * 60)
        result = tpcc_thread.get_result()
        self.log.info(result)
        self.assertIn(self.Constant.TPCC_SUCCESS_MSG, result)

        self.log.info('--------------4.查看数据库状态是否正常------------------------')
        result = self.commonshpri.getDbClusterStatus("status")
        self.log.info(result)
        self.assertTrue(result)

        self.log.info('-------------------------查看gaussdb.state二进制文件-------------')
        shell_cmd = f"cat {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')}"
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)

        self.log.info('-------------------5.重启集群-----------------')
        result = self.commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = self.commonshpri.startDbCluster()
        self.assertTrue(result)

        self.log.info('---------------------------6.查看gaussdb.state状态--------------------------')
        shell_cmd = f"find {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')} -type f -mmin -1"
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)
        self.assertIn('gaussdb.state', result)

        self.log.info('-------------------------查看gaussdb.state二进制文件-------------')
        shell_cmd = f"ls -l --full-time {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')}; cat {os.path.join(macro.DB_INSTANCE_PATH, 'gaussdb.state')}"
        self.log.info(shell_cmd)
        result = self.dbPrimaryDbUser.sh(shell_cmd).result()
        self.log.info(result)
        self.assertTrue(self.addtxt not in result)

    def tearDown(self):
        self.log.info('-------------------------环境清理------------------------')
        self.log.info('-------------------重启集群-----------------')
        self.commonshpri.stopDbCluster()
        self.commonshpri.startDbCluster()

        self.log.info('----------Opengauss_Reliability_Dbsys_Case093.py 执行结束-----------------------')
